package com.my.mycommoncore.utils;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class TimestampUtil {
    /**
     * 获取当前时间戳(秒级)
     * @return 当前时间戳
     */
    public static long getCurrentSeconds(){
        return Instant.now().getEpochSecond();
    }

    /**
     * 获取当前时间戳(毫秒级)
     * @return  当前时间戳
     */
    public static long getCurrentMillis(){
        return Instant.now().toEpochMilli();
    }

    /**
     * 获取未来x秒的时间戳
     * @param seconds 秒
     * @return 时间戳
     */
    public static long getSecondLaterSeconds(long seconds){
        //获取当前时间
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        //在之前获取时间的基础上加多少秒
        ZonedDateTime secondLater = now.plusSeconds(seconds);
        //将时间转换为时间戳
        return secondLater.toEpochSecond();
    }

    /**
     * 获取未来x毫秒的时间戳(毫秒级)
     * @param seconds 秒
     * @return 时间戳
     */
    public static long getSecondsLaterMillis(long seconds) {
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        ZonedDateTime secondsLater = now.plusSeconds(seconds);
        return secondsLater.toInstant().toEpochMilli();
    }

    /**
     * 获取未来x天的时间戳(秒级)
     * @param days 天
     * @return 时间戳
     */
    public static long getDaysLaterSeconds(long days){
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        ZonedDateTime secondLater =now.plusDays(days);
        return secondLater.toEpochSecond();
    }

    /**
     * 获取未来x天的时间戳
     * @param days 天数
     * @return 时间戳
     */
    public static long getDaysLaterMillis(long days) {
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        ZonedDateTime secondLater =now.plusDays(days);
        return secondLater.toInstant().toEpochMilli();
    }

    /**
     * 获取未来x月的时间戳 (秒级)
     * @param months  月
     * @return  时间戳
     */
    public static long getMonthsLaterSeconds(long months){
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        ZonedDateTime secondLater =now.plusMonths(months);
        return secondLater.toEpochSecond();
    }

    /**
     * 获取未来x月的时间戳(毫秒级)
     * @param months 月
     * @return 时间戳
     */
    public static long getMonthsLaterMillis(long months) {
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        ZonedDateTime secondLater =now.plusMonths(months);
        return secondLater.toInstant().toEpochMilli();
    }

    /**
     * 获取未来x年的时间戳(秒级)
     * @param years 年
     * @return  时间戳
     */
    public static long getYearLaterSeconds(long years){
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        ZonedDateTime secondLater =now.plusYears(years);
        return secondLater.toEpochSecond();
    }
    /**
     * 获取未来x年的时间戳(毫秒级)
     * @param years 年
     * @return  时间戳
     */
    public static long getYearLaterMillis(long years) {
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
        ZonedDateTime secondLater =now.plusYears(years);
        return secondLater.toInstant().toEpochMilli();
    }

    /**
     * 计算两个时间戳的毫秒差值
     * @param timestamp1 时间戳1
     * @param timestamp2 时间戳2
     * @return  时间戳差异(毫秒)
     */
    public static long calculateDifferenceMillis(long timestamp1, long timestamp2) {
        //Instant.ofEpochMilli(timestamp1)：将一个毫秒级时间戳（timestamp1，通常是long类型）转换为Instant对象
        return ChronoUnit.MILLIS.between(
                Instant.ofEpochMilli(timestamp1),
                Instant.ofEpochMilli(timestamp2));
    }
    /**
     * 计算两个时间戳的差值秒级
     * @param timestamp1 时间戳1
     * @param timestamp2 时间戳2
     * @return  时间戳差异(毫秒)
     */
    public static long calculateDifferenceSeconds(long timestamp1, long timestamp2) {
        return ChronoUnit.SECONDS.between(Instant.ofEpochSecond(timestamp1),Instant.ofEpochSecond(timestamp2));
    }


}
